package com.supplychainapi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.joindata.inf.common.basic.exceptions.ServiceException;
import com.supplychainapi.common.BizCode;
import com.supplychainapi.common.enums.ExceptionMessage;
import com.supplychainapi.dao.prod.ScmTbstorageMapper;
import com.supplychainapi.model.ScmTbseqno;
import com.supplychainapi.model.ScmTbstorage;
import com.supplychainapi.model.ScmTbstorageExtend;
import com.supplychainapi.param.ColTitle;
import com.supplychainapi.service.IScmTbstorageService;
import com.supplychainapi.service.TbMaterialService;
import com.supplychainapi.service.TbSeqNoService;
import com.supplychainapi.utils.Constants;
import com.supplychainapi.utils.StringUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 仓库
 *
 * @author:xiezhiwei
 * @email:xiezhiwei@niwodai.net
 * @date:2017-10-11
 */
@Service
public class ScmTbstorageServiceImpl implements IScmTbstorageService {

    @Autowired
    TbSeqNoService tbSeqNoService;
    private Logger logger = LogManager.getLogger(ScmTbstorageServiceImpl.class);
    @Autowired
    private ScmTbstorageMapper scmTbstorageMapper;

    @Autowired
    private TbMaterialService materialService;

    @Override
    public void addScmTbstorageInfo(ScmTbstorage storage) throws ServiceException {
        logger.info("新增仓库信息");
        if (StringUtil.checkIsEmoji(storage.getFsStorageName())) {
            throw ExceptionMessage.emoji_failed.getServiceException();
        }
        if (storage.getFsStorageId() == null || storage.getFsStorageId().trim().equals("")) {
            logger.info("----开始自动生成仓库编号-----");
            ScmTbseqno model = new ScmTbseqno();
            model.setFsShopGUID(storage.getFsShopGUID());
            model.setFsCls(Constants.SCM_TBSTORAGE);
            String fsStorageId = tbSeqNoService.selectSeqNo(model);
            storage.setFsStorageId(fsStorageId);
        } else {
            logger.info("手动填写编号业务处理");
            ScmTbseqno tbseqno1 = new ScmTbseqno();
            tbseqno1.setFiSeqNo(storage.getFsStorageId());
            tbseqno1.setFsCls(Constants.SCM_TBSTORAGE);
            tbseqno1.setFsShopGUID(storage.getFsShopGUID());
            tbSeqNoService.checkCode(tbseqno1);
            tbSeqNoService.insert(tbseqno1, storage.getFsShopGUID());

        }
        storage.setFsUpdateTime(new Date());
        scmTbstorageMapper.insertSelective(storage);
    }

    @Override
    public PageInfo getScmTbstorageListInfo(String fsShopGUID, String fsStorageName, Integer pageNum, Integer pageSize) throws ServiceException {
        logger.info("fsShopGUID={}", fsShopGUID);
        List<ScmTbstorageExtend> listScmTbstorage = scmTbstorageMapper.getTbstorageListInfo(fsShopGUID, fsStorageName);

        PageHelper.startPage(pageNum, pageSize);
        //PageHelper.orderBy("fsCreateTime DESC");
        return new PageInfo(listScmTbstorage);

    }

    @Override
    public List<ScmTbstorage> getScmTbstorageListInfo(String fsShopGUID, Integer page, Integer pageSize)
            throws ServiceException {
        logger.info("fsShopGUID={}", fsShopGUID);
        if (page != null) {
            page = (page - 1) * pageSize;
        } else {
            page = 0;
        }
        List<ScmTbstorage> listScmTbstorage = scmTbstorageMapper.getScmTbstorageListInfo(fsShopGUID, null, page, pageSize);
        return listScmTbstorage;
    }

    @Override
    public List<ScmTbstorage> getScmTbstorageLikeListInfo(String fsShopGUID, String fsStorageName, Integer page, Integer pageSize)
            throws ServiceException {
        logger.info("fsShopGUID={},fsStorageName={},page={},pageSize={}", fsShopGUID, fsStorageName, page, pageSize);
        if (page != null) {
            page = (page - 1) * pageSize;
        } else {
            page = 0;
        }
        List<ScmTbstorage> listScmTbstorage = scmTbstorageMapper.getScmTbstorageLikeListInfo(fsShopGUID, fsStorageName, page, pageSize);
        return listScmTbstorage;
    }


    @Override
    public ScmTbstorage getScmTbstorageByFsShopGUIDAndFsStorageIdInfo(String fsShopGUID, String fsStorageId)
            throws ServiceException {
        logger.info("fsShopGUID={},fsStorageId={}", fsShopGUID, fsStorageId);
        ScmTbstorage storage = scmTbstorageMapper.getScmTbstorageByFsShopGUIDAndFsStorageIdInfo(fsShopGUID, fsStorageId);
        return storage;
    }

    @Override
    public Boolean getStorageDeDuplication(String fsShopGUID, String fsStorageName, String fsStorageId, String type) throws ServiceException {
        logger.info("判断仓库名称是否重复 fsShopGUID={},fsStorageName={},fsStorageId={},type={}", fsShopGUID, fsStorageName, fsStorageId, type);
        Integer countNumber = scmTbstorageMapper.getStorageDeDuplication(fsShopGUID, fsStorageName);
        if (type.equals("update")) {
            //当前传入的仓库名称和数据库查询出来的名称相同不做处理
            ScmTbstorage scmStorage = scmTbstorageMapper.getScmTbstorageByFsShopGUIDAndFsStorageIdInfo(fsShopGUID, fsStorageId);
            if (scmStorage != null && scmStorage.getFsStorageName().equals(fsStorageName)) {
                return false;
            }
        }
        logger.info("根据当前门店和仓库名查询返回结果集=" + countNumber);
        return countNumber != null && countNumber > 0;
    }

    @Override
    public void deleteStorageInfo(String fsShopGUID, String fsStorageId) throws ServiceException {
        logger.info("---------------删除仓库开始 fsShopGUID={}, fsStorageId={}-----------", fsShopGUID, fsStorageId);
        Integer count = materialService.selectByWarehouseId(fsStorageId, fsShopGUID);
        logger.info("查询仓库是否被引用 count = " + count);
        if (count != null && count > 0) {
            throw new ServiceException(BizCode.FAIL.getNum(), BizCode.STORAGE_QUOTE_ERROR_MESSAGE.getDesc());
        }
        Integer deleteCount = scmTbstorageMapper.deleteStorageInfo(fsShopGUID, fsStorageId);
        logger.info("仓库删除 deleteCount ={}", deleteCount);
    }

    @Override
    public void updateStorageInfo(ScmTbstorage storage) throws ServiceException {
        //int updateCount = scmTbstorageMapper.updateByPrimaryKeySelective(storage);
        Integer updateCount = scmTbstorageMapper.updateStorageInfo(storage);
        logger.info("更新仓库信息  updateCount ={}", updateCount);
    }

    @Override
    public int count(ScmTbstorage param) {
        return scmTbstorageMapper.selectCount(param);
    }

    @Override
    public PageInfo getChoiceStorageListInfo(String fsShopGUID, String fsStorageName, Integer pageNum, Integer pageSize) throws ServiceException {
        logger.info("fsShopGUID={},fsStorageName={}", fsShopGUID, fsStorageName);
        PageHelper.startPage(pageNum, pageSize);
        //PageHelper.orderBy("fsCreateTime DESC");
        List<ScmTbstorage> storageList = scmTbstorageMapper.getChoiceStorageListInfo(fsShopGUID, fsStorageName);
        return new PageInfo(storageList);
    }

    @Override
    public List<ColTitle> getSchema() {
        List<ColTitle> list = new ArrayList<ColTitle>();
        list.add(new ColTitle("fsStorageId", "仓库代码", "center", 100));
        list.add(new ColTitle("fsStorageName", "仓库名称", "center", 100));
        list.add(new ColTitle("fsStaffId", "仓管人员", "center", 100));
        list.add(new ColTitle("fiDirect", "是否直拨", "center", 100));
        list.add(new ColTitle("fiStatus", "状态", "center", 100));
        list.add(new ColTitle("__EDIT__", "操作", null, 150, "fsSupplierId"));
        return list;
    }

    @Override
    public List<ColTitle> getChoiceSchema() {
        List<ColTitle> list = new ArrayList<ColTitle>();
        list.add(new ColTitle("fsStorageId", "仓库代码", "center", 100));
        list.add(new ColTitle("fsStorageName", "仓库名称", "center", 100));
        list.add(new ColTitle("fsStaffId", "仓管人员", "center", 100));
        list.add(new ColTitle("fiDirect", "是否直拨", "center", 100));
        return list;
    }

    @Override
    public ScmTbstorageExtend getStorageExtendInfo(String fsShopGUID, String fsStorageId) throws ServiceException {
        logger.info("fsShopGUID={},fsStorageId={}", fsShopGUID, fsStorageId);
        ScmTbstorageExtend storageExtend = scmTbstorageMapper.getStorageExtendInfo(fsShopGUID, fsStorageId);
        return storageExtend;
    }


}
